let fs = require('fs');
let path = require('path');
let sizeOf = require('image-size');
let os = require('os');
// 全局变量  系统聚焦图片路径
let sourcePath = path.join(os.homedir(), '\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets');;
let destPath = path.join(os.homedir(), '\\Pictures\\Windows聚焦\\');

// 系统可用聚焦图片的数组
let item_list = new Map();
// 用户当前选择的图片的数组
let select_list = new Map();
// 全选按钮状态
let is_select_all = false;

let $jqBody = $('#list');
fileList(sourcePath);
/**
 * 递归函数，提取目录下的文件
 * @param string filePath 
 */
function fileList(filePath) {
    let number = 0;
    let files = fs.readdirSync(filePath);
    files.forEach((file, index) => {
        let fPath = path.join(filePath, file);
        let fileStatus = fs.statSync(fPath);
        if (fileStatus.isDirectory()) {
            fileList(fPath);
        } else {
            try {
                let fileName = fPath;
                let dimensions = sizeOf(fileName);
                if ((dimensions.width == 1920 && dimensions.height == 1080) || (dimensions.height == 1920 && dimensions.width == 1080)) {
                    let $jqItem = $(`<div class="col-xs-6 col-sm-3 col-md-2 item"><img class="img-thumbnail" src="${fileName}" id="img_${number}"></div>`);
                    $jqItem.appendTo($jqBody);
                    $('#list').on('click', `#img_${number}`, function () {
                        let cur_select = $(this).attr('id');
                        let find_idx = select_list.has(cur_select);
                        //console.log(find_idx);
                        if (find_idx) {
                            select_list.delete(cur_select);
                            $(this).removeClass('active');
                        } else {
                            select_list.set(cur_select, [file, fileName]);
                            $(this).addClass('active');
                        }
                        $('#select_count').text(select_list.size);
                        //console.log(select_list);
                    });
                    item_list.set(`img_${number}`, [file, fileName]);
                    number++;
                }
            } catch (e) {
                //console.warn(e.message.warn);
            }
        }
    });
    $('#total').text(number);
}
/**
 * 全选按钮的事件处理函数
 */
$('#link_select_all').on('click', function (e) {
    e.preventDefault();
    is_select_all = !is_select_all;
    if (is_select_all) {
        select_list = item_list;
        $('#list img').addClass('active');
        $(this).text('取消全选');
    } else {
        select_list.clear();
        $('#list img').removeClass('active');
        $(this).text('全选');
    }
    // console.log(select_list);
});
$('#link_export').on('click', function (e) {
    e.preventDefault();
    let dir = fs.existsSync(destPath);
    if (!dir) {
        fs.mkdirSync(destPath);
    }
    let idx = 0;
    select_list.forEach((item) => {
        idx++;
        try {
            let read = fs.readFileSync(item[1]);
            fs.writeFileSync(destPath + item[0] + '.jpg', read);
        } catch(e) {
            console.error(e)
        }
        //console.log(item);
    });
    alert('已成功导出图片到用户图片目录下');
    //console.log(idx);
});
